import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class CalculatorPanel extends JPanel implements ActionListener{
	JPanel displayPanel;
	JPanel buttonPanel;
	JLabel  txtScreen;
	JButton btnMC;
	JButton btnMR;
	JButton btnMS;
	JButton btnMPlus;
	JButton btnMMinus;
	JButton btnBack;
	JButton btnCE;
	JButton btnC;
	JButton btnCPlusMinus;
	JButton btnSQRT;
	
	JButton btn0;
	JButton btn1;
	JButton btn2;
	JButton btn3;
	JButton btn4;
	JButton btn5;
	JButton btn6;
	JButton btn7;
	JButton btn8;
	JButton btn9;
	
	JButton btnMinus;
	JButton btnAdd;
	JButton btnMul;
	JButton btnDiv;	
	JButton btnDivX;
	JButton btnPer;
	JButton btnDot;
	JButton btnEqual;
	
	String display;
	double answer;
	double a;
	int func;
	int funcOld;
	double m;
	boolean isMemory;
	boolean isEqual = false;
	
	void reset()
	{
		display = "0";
		answer = 0;
		a = 0;
		func = -1;
		txtScreen.setText(display);
	}
	
	CalculatorPanel()
	{
		setLayout(new BorderLayout());
		displayPanel = new JPanel();
		txtScreen = new JLabel();
		displayPanel.add(txtScreen);
		displayPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		
		
		txtScreen.setAlignmentX(JTextArea.RIGHT_ALIGNMENT);

		txtScreen.setText(display);
		buttonPanel = new JPanel(new GridBagLayout());
		add(displayPanel,BorderLayout.NORTH);
		add(buttonPanel,BorderLayout.CENTER);
		
		
		
		btnMC = new JButton("MC");
		btnMR = new JButton("MR");
		btnMS = new JButton("MS");
		btnMPlus = new JButton("M+");
		btnMMinus = new JButton("M-");
		btnBack =new JButton("<-");
		btnCE =new JButton("CE");
		btnC = new JButton("C");
		btnCPlusMinus = new JButton("+-");
		btnSQRT = new JButton("Sq");
		
		btn0 = new JButton("0");
		btn1 = new JButton("1");
		btn2 = new JButton("2");
		btn3 = new JButton("3");
		btn4 = new JButton("4");
		btn5 = new JButton("5");
		btn6 = new JButton("6");
		btn7 = new JButton("7");
		btn8 = new JButton("8");
		btn9 = new JButton("9");
		
		btnMinus = new JButton("-");
		btnAdd = new JButton("+");
		btnMul = new JButton("*");
		btnDiv = new JButton("/");
		btnDivX = new JButton("1/x");
		btnPer = new JButton("%");
		btnEqual = new JButton("=");
		btnDot = new JButton(".");
		GridBagConstraints c = new GridBagConstraints();
		
		
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 0;
		c.gridy = 0;
		buttonPanel.add(btnMC, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 1;
		c.gridy = 0;
		buttonPanel.add(btnMR, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 2;
		c.gridy = 0;
		buttonPanel.add(btnMS, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 3;
		c.gridy = 0;
		buttonPanel.add(btnMPlus, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 4;
		c.gridy = 0;
		buttonPanel.add(btnMMinus, c);
		
		//line 2
		// btnBack; btnCE; btnC; btnCPlusMinus; btnSQRT;
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 0;
		c.gridy = 1;
		buttonPanel.add(btnBack, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 1;
		c.gridy = 1;
		buttonPanel.add(btnCE, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 2;
		c.gridy = 1;
		buttonPanel.add(btnC, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 3;
		c.gridy = 1;
		buttonPanel.add(btnCPlusMinus, c);
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.gridx = 4;
		c.gridy = 1;
		buttonPanel.add(btnSQRT, c);
		//line 3
		// btnBack; btnCE; btnC; btnCPlusMinus; btnSQRT;
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 0;
		c.gridy = 2;
		buttonPanel.add(btn7, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 1;
		c.gridy = 2;
		buttonPanel.add(btn8, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 2;
		c.gridy = 2;
		buttonPanel.add(btn9, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 3;
		c.gridy = 2;
		buttonPanel.add(btnDiv, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 4;
		c.gridy = 2;
		buttonPanel.add(btnPer, c);
		//line 4
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.gridx = 0;
		c.gridy = 3;
		buttonPanel.add(btn4, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 1;
		c.gridy = 3;
		buttonPanel.add(btn5, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 2;
		c.gridy = 3;
		buttonPanel.add(btn6, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 3;
		c.gridy = 3;
		buttonPanel.add(btnMul, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 4;
		c.gridy = 3;
		buttonPanel.add(btnDivX, c);
		
		//line 5
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 0;
		c.gridy = 4;
		buttonPanel.add(btn1, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 1;
		c.gridy = 4;
		buttonPanel.add(btn2, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 2;
		c.gridy = 4;
		buttonPanel.add(btn3, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 3;
		c.gridy = 4;
		buttonPanel.add(btnMinus, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.weighty = 0.5;
		c.gridx = 4;
		c.gridy = 4;
		c.gridheight=2;
		buttonPanel.add(btnEqual, c);
		
		//line 6
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 2;
		//c.weighty = 0.5;
		c.gridx = 0;
		c.gridy = 5;
		c.gridwidth = 2;
		buttonPanel.add(btn0, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 2;
		c.gridwidth = 1;
		c.gridy = 5;
		buttonPanel.add(btnDot, c);
		c.fill = GridBagConstraints.BOTH;
		//c.weightx = 1;
		c.gridx = 3;
		c.gridy = 5;
		buttonPanel.add(btnAdd, c);
		
		
		btn0.addActionListener(this);
		btn1.addActionListener(this);
		btn2.addActionListener(this);
		btn3.addActionListener(this);
		btn4.addActionListener(this);
		btn5.addActionListener(this);
		btn6.addActionListener(this);
		btn7.addActionListener(this);
		btn8.addActionListener(this);
		btn9.addActionListener(this);
		btnCPlusMinus.addActionListener(this);
		btnDot.addActionListener(this);
		btnAdd.addActionListener(this);
		btnBack.addActionListener(this);
		btnC.addActionListener(this);
		btnCE.addActionListener(this);
		btnDiv.addActionListener(this);
		btnDivX.addActionListener(this);
		btnEqual.addActionListener(this);
		btnMC.addActionListener(this);
		btnMinus.addActionListener(this);
		btnMMinus.addActionListener(this);
		btnMPlus.addActionListener(this);
		btnMR.addActionListener(this);
		btnMS.addActionListener(this);
		btnMul.addActionListener(this);
		btnPer.addActionListener(this);
		btnSQRT.addActionListener(this);
		
		reset();
	}
	
	private double calculate(double answer, double a, int func)
	{
		switch (func)
		{
		case 0:
			return answer + a;
		case 1:
			return answer - a;
		case 2:
			return answer * a;
		case 3:
			return answer / a;
		default:
			return a;
		}
	}

	private void showError()
	{
		txtScreen.setText("ERROR");
		reset();
	}
	
	private void Button0()
	{
		if(display == "0" || isEqual == true)
		{
			txtScreen.setText(display);
			isEqual = false;
		}
		else
		{
			display+="0";
			txtScreen.setText(display);
		}
	}
	
	private void Button1()
	{
		if(display == "0" || isEqual == true)
		{
			isEqual = false;
			display = "1";
			txtScreen.setText(display);
		}
		else
		{
			display+="1";
			txtScreen.setText(display);
		}
	}
	
	private void Button2()
	{
		if(display == "0" || isEqual == true)
		{
			isEqual = false;
			display = "2";
			txtScreen.setText(display);
		}
		else
		{
			display+="2";
			txtScreen.setText(display);
		}
	}
	
	private void Button3()
	{
		if(display == "0" || isEqual == true)
		{
			isEqual = false;
			display = "3";
			txtScreen.setText(display);
		}
		else
		{
			display+="3";
			txtScreen.setText(display);
		}
	}
	
	private void Button4()
	{
		if(display == "0" || isEqual == true)
		{
			isEqual = false;
			display = "4";
			txtScreen.setText(display);
		}
		else
		{
			display+="4";
			txtScreen.setText(display);
		}
	}
	
	private void Button5()
	{
		if(display == "0" || isEqual == true)
		{
			isEqual = false;
			display = "5";
			txtScreen.setText(display);
		}
		else
		{
			display+="5";
			txtScreen.setText(display);
		}
	}
	
	private void Button6()
	{
		if(display == "0" || isEqual == true)
		{
			isEqual = false;
			display = "6";
			txtScreen.setText(display);
		}
		else
		{
			display+="6";
			txtScreen.setText(display);
		}
	}
	
	private void Button7()
	{
		if(display == "0" || isEqual == true)
		{
			isEqual = false;
			display = "7";
			txtScreen.setText(display);
		}
		else
		{
			display+="7";
			txtScreen.setText(display);
		}
	}
	
	private void Button8()
	{
		if(display == "0" || isEqual == true)
		{
			isEqual = false;
			display = "8";
			txtScreen.setText(display);
		}
		else
		{
			display+="8";
			txtScreen.setText(display);
		}
	}
	
	private void Button9()
	{		
		if(display == "0" || isEqual == true)
		{
			isEqual = false;
			display = "9";
			txtScreen.setText(display);
		}
		else
		{
			display+="9";
			txtScreen.setText(display);
		}
	}
	
	private void ButtonDot()
	{
		display+=".";
		txtScreen.setText(display);
	}
	
	private void ButtonMPlusMinus()
	{
		if(display == "0" || isEqual == true)
		{
			isEqual = false;
			display = "-";
		}
		else
		{
			String temp = "";
			if(display.charAt(0) != '-')
				temp = "-" + display;
			else
				temp = String.copyValueOf(display.toCharArray(), 1, display.length() - 1);
			display = temp;
		}
		txtScreen.setText(display);
	}
	
	private void ButtonBack()
	{
		String temp = display.substring(0, display.length() - 1);
		if(temp.length() == 0)
			display = "0";
		else
			display = temp;
		txtScreen.setText(display);
	}
	
	private void ButtonCE()
	{
		display = "0";
	}
	
	private void ButtonC()
	{
		reset();
	}
	
	// Func = 0;
	private void ButtonAdd()
	{
		try
		{
			String temp = "";
			a = Double.valueOf(txtScreen.getText());
			if(func < 0)
			{
				answer = a;
				temp = display;
			}
			else
			{
				answer = calculate(answer, a, func);
				temp = String.valueOf(answer);
			}
			func = 0;
			display = "0";
			txtScreen.setText(temp);			
		}
		catch (Exception ex)
		{
			showError();
		}
	}
	
	// Func = 1;
	private void ButtonMinus()
	{
		try
		{
			String temp = "";
			a = Double.valueOf(display);
			if(func < 0)
			{
				answer = a;
				temp = display;
			}
			else
			{
				answer = calculate(answer, a, func);
				temp = String.valueOf(answer);
			}
			func = 1;
			display = "0";
			txtScreen.setText(temp);			
		}
		catch (Exception ex)
		{
			showError();
		}
	}
	
	// Func = 2;
	private void ButtonMul()
	{
		try
		{
			String temp = "";
			a = Double.valueOf(display);
			if(func < 0)
			{
				answer = a;
				temp = display;
			}
			else
			{
				answer = calculate(answer, a, func);
				temp = String.valueOf(answer);
			}
			func = 2;
			display = "0";
			txtScreen.setText(temp);			
		}
		catch (Exception ex)
		{
			showError();
		}
	}
	
	// Func = 3;
	private void ButtonDiv()
	{
		try
		{
			String temp = "";
			a = Double.valueOf(display);
			if(func < 0)
			{
				answer = a;
				temp = display;
			}
			else
			{
				answer = calculate(answer, a, func);
				temp = String.valueOf(answer);
			}
			func = 3;
			display = "0";
			txtScreen.setText(temp);			
		}
		catch (Exception ex)
		{
			showError();
		}
	}
	
	private void ButtonSqrt()
	{
		double temp = Double.valueOf(txtScreen.getText());
		temp = Math.sqrt(temp);
		String a = String.valueOf(temp);
		display = "0";
		txtScreen.setText(a);
		
	}
	
	private void ButtonPer()
	{
		double temp = Double.valueOf(txtScreen.getText());
		temp = a* temp/100;
		String a = String.valueOf(temp);
		display = "0";
		txtScreen.setText(a);
		
	}
	
	private void ButtonDivX()
	{
		try
		{		
			double temp = Double.valueOf(txtScreen.getText());
			temp = 1 / temp;			
			display = String.valueOf(temp);
			txtScreen.setText(display);
		}
		catch (Exception ex)
		{
			showError();
		}
		display = "0";
	}
	
	private void ButtonMC()
	{
		isMemory = false;
		m = 0;
	}
	
	private void ButtonMR()
	{
		try
		{
			if (isMemory)
			{
				display = String.valueOf(m);
				txtScreen.setText(display);
			}
		}
		catch (Exception ex)
		{
			showError();
		}
	}
	
	private void ButtonMS()
	{
		try
		{
			m = Double.valueOf(txtScreen.getText());
			isMemory = true;
		}
		catch (Exception ex)
		{
			showError();
		}
	}
	
	private void ButtonMPlus()
	{
		try
		{
			if (isMemory)
			{
				m += Double.valueOf(txtScreen.getText());
				if (m == 0)
					isMemory = false;
			}
			else
			{
				isMemory = true;
				m = Double.valueOf(txtScreen.getText());
			}
		}
		catch(Exception ex)
		{
			showError();
		}
	}
	
	private void ButtonMMinus()
	{
		try
		{
			if (isMemory)
			{
				m -= Double.valueOf(txtScreen.getText());
				if (m == 0)
					isMemory = false;
			}
			else
			{
				isMemory = true;
				m = Double.valueOf(txtScreen.getText());
			}
		}
		catch(Exception ex)
		{
			showError();
		}
	}
	
	private void ButtonEqual()
	{
		if (!isEqual)
		{
			a = Double.valueOf(txtScreen.getText());
			answer = calculate(answer, a, func);
			display = String.valueOf(answer);
			txtScreen.setText(display);
			funcOld = func;
			func = -1;
			isEqual = true;
		}
		else
		{
			answer = calculate(answer, a, funcOld);
			txtScreen.setText(String.valueOf(answer));
		}
	}
	
	public void actionPerformed(ActionEvent e)
	{		
		if(e.getSource() == btn0)
		{
			Button0();
		}
		else if(e.getSource() == btn1)
		{
			Button1();
		}
		else if(e.getSource() == btn2)
		{
			Button2();
		}
		else if(e.getSource() == btn3)
		{
			Button3();
		}
		else if(e.getSource() == btn4)
		{
			Button4();
		}
		else if(e.getSource() == btn5)
		{
			Button5();
		}
		else if(e.getSource() == btn6)
		{
			Button6();
		}
		else if(e.getSource() == btn7)
		{
			Button7();
		}
		else if(e.getSource() == btn8)
		{
			Button8();
		}
		else if(e.getSource() == btn9)
		{
			Button9();
		}
		else if(e.getSource() == btnDot)
		{
			ButtonDot();
		}
		else if(e.getSource() == btnCPlusMinus)
		{
			ButtonMPlusMinus();
		}
		else if(e.getSource() == btnBack)
		{
			ButtonBack();
		}
		else if(e.getSource() == btnCE)
		{
			ButtonCE();
		}
		else if(e.getSource() == btnC)
		{
			ButtonC();
		}
		else if(e.getSource() == btnMinus)
		{
			ButtonMinus();
		}
		else if(e.getSource() == btnAdd)
		{
			ButtonAdd();
		}
		else if(e.getSource() == btnDiv)
		{
			ButtonDiv();
		}
		else if(e.getSource() == btnMul)
		{
			ButtonMul();
		}
		else if(e.getSource() == btnSQRT)
		{
			ButtonSqrt();
		}
		else if(e.getSource() == btnPer)
		{
			ButtonPer();
		}
		else if(e.getSource() == btnDivX)
		{
			ButtonDivX();
		}
		else if(e.getSource() == btnMC)
		{
			ButtonMC();
		}
		else if(e.getSource() == btnMR)
		{
			ButtonMR();
		}
		else if(e.getSource() == btnMS)
		{
			ButtonMS();
		}
		else if(e.getSource() == btnMPlus)
		{
			ButtonMPlus();
		}
		else if(e.getSource() == btnMMinus)
		{
			ButtonMMinus();
		}
		else if(e.getSource() == btnEqual)
		{
			ButtonEqual();
		}
	}
}
